package org.example.order.controller;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.order.common.APIResponse;
import org.example.order.common.ResponseCode;
import org.example.order.entity.Comment;
import org.example.order.service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/add")
    public APIResponse add(@RequestBody Comment comment) {
        commentService.add(comment);
        return new APIResponse(ResponseCode.SUCCESS, "添加成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public APIResponse deleteById(@PathVariable Long id) {
        commentService.deleteById(id);
        return new APIResponse(ResponseCode.SUCCESS, "删除成功");
    }

    @DeleteMapping("/deleteBatch")
    public APIResponse deleteBatch(@RequestBody List<Long> ids) {
        commentService.deleteBatch(ids);
        return new APIResponse(ResponseCode.SUCCESS, "批量删除成功");
    }

    @PutMapping("/update")
    public APIResponse update(@RequestBody Comment comment) {
        commentService.update(comment);
        return new APIResponse(ResponseCode.SUCCESS, "更新成功");
    }

    @GetMapping("/selectById/{id}")
    public APIResponse selectById(@PathVariable Long id) {
        Comment comment = commentService.selectById(id);
        return new APIResponse(ResponseCode.SUCCESS, "查询成功", comment);
    }

    @GetMapping("/selectAll")
    public APIResponse selectAll() {
        List<Comment> list = commentService.selectAll();
        return new APIResponse(ResponseCode.SUCCESS, "查询成功", list);
    }

    @GetMapping("/selectPage")
    public APIResponse selectPage(
            Comment comment,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Comment> pageInfo = commentService.selectPage(comment, pageNum, pageSize);
        return new APIResponse(ResponseCode.SUCCESS, "分页查询成功", pageInfo);
    }

    @GetMapping("/selectByUserIdOrGoodIdOrGoodName")
    public APIResponse selectByUserIdOrGoodIdOrGoodName(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long goodId,
            @RequestParam(required = false) String goodName) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("goodId", goodId);
        params.put("goodName", goodName);
        List<Comment> list = commentService.selectByUserIdOrGoodIdOrGoodName(params);
        return new APIResponse(ResponseCode.SUCCESS, "查询成功", list);
    }
}